package org.leaf.green.b2b

import org.springframework.transaction.annotation.*
import org.hibernate.FetchMode as FM

class DistributionService {

    static transactional = true

    def init() {
        def distributionInstance = new Distribution()
            //set serialnumber
            distributionInstance.serialNumber = "${new Date().format('yyyyMMdd')}000"
            //set approved 
            distributionInstance.approved = false
          
        //add details
        def begin_serialNumber = 0
        Product.listOrderBySerialNumber().each{ product ->
            begin_serialNumber += 1
            distributionInstance.addToDetails(
                new DistributionDetail(
                      serialNumber : String.format('%03d', begin_serialNumber)
                    , product : product
                    , unit : '斤'
                    , quantity : 0
                    , price : 0
                    , retailPrice : 0
                    , amount : 0
                    , retailAmount : 0
                    , packageUsed : 0
                    , packageReturned : 0 
                )
            )
        }
           
        return distributionInstance
    }
  
}
